package org.happycomp.radio;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Jedna polozka playlistu
 * @author pavels
 */
public class PlaylistEntry {

	public static final java.util.logging.Logger LOGGER = java.util.logging.Logger
			.getLogger(PlaylistEntry.class.getName());
	
	private Station station;
	
	private URL url;
	private String title;
	private long length;
	
	public PlaylistEntry(Station st) {
		super();
		this.station = st;
	}

	protected PlaylistEntry() {
		super();
	}


	/**
	 * Vraci url
	 * @return URL
	 */
	public URL getUrl() {
		return url;
	}


	/**
	 * Vraci titulek
	 * @return
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * Vraci velikost
	 * @return
	 */
	public long getLength() {
		return length;
	}

	public void setUrl(URL url) {
		this.url = url;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	
	
	public Station getStation() {
		return station;
	}


	protected void setStation(Station station) {
		this.station = station;
	}

	public void setLength(long length) {
		this.length = length;
	}

	public void init(String leftSubString, String rightSubString) throws MalformedURLException {
		if (leftSubString.toLowerCase().startsWith("length")) {
			this.length = Integer.parseInt(rightSubString);
		}
		if (leftSubString.toLowerCase().startsWith("file")) {
			this.url = new URL(rightSubString);
		}
		if (leftSubString.toLowerCase().startsWith("title")) {
			this.title = rightSubString;
		}
	}

	public Element storeElement(Document document) {
		Element playlist = document.createElement("playlist");
		
		Element list = document.createElement("title");
		list.setTextContent(this.title);
		playlist.appendChild(list);
		
		Element length = document.createElement("length");
		length.setTextContent(this.length+"");
		playlist.appendChild(length);
		
		
		Element station = document.createElement("station");
		station.setTextContent(this.station.name());
		playlist.appendChild(station);

		Element url = document.createElement("url");
		url.setTextContent(this.url.toString());
		playlist.appendChild(url);

		return playlist;
	}

	public static PlaylistEntry loadFromElement(Element elm) {
		if (elm.getNodeName().equals("playlist")) {
			try {

				PlaylistEntry entry = new PlaylistEntry();
				NodeList childNodes = elm.getChildNodes();
				for (int i = 0,ll=childNodes.getLength(); i < ll; i++) {
					Node node = childNodes.item(i);
					if (node.getNodeType() == Node.ELEMENT_NODE) {
						String name = node.getNodeName();
						if (name.equals("title")) {
							entry.setTitle(node.getTextContent());
						}
						if (name.equals("length")) {
							entry.setLength(Long.parseLong(node.getTextContent()));
						}
						if (name.equals("station")) {
							entry.setStation(Station.valueOf(node.getTextContent()));
						}
						if (name.equals("url")) {
							entry.setUrl(new URL(node.getTextContent()));
						}
					}
				}
				return entry;
			} catch (NumberFormatException e) {
				LOGGER.log(Level.SEVERE, e.getMessage(), e);
			} catch (DOMException e) {
				LOGGER.log(Level.SEVERE, e.getMessage(), e);
			} catch (MalformedURLException e) {
				LOGGER.log(Level.SEVERE, e.getMessage(), e);
			}
		}
		return null;
	}

	@Override
	public String toString() {
		return "PlaylistEntry [station=" + station + ", url=" + url
				+ ", title=" + title + ", length=" + length + "]";
	}

	
}

